<?php
/**
 * Created by PhpStorm.
 * User: 陈艺赫
 * Date: 2017/12/8
 * Time: 0:40
 */

namespace Admin\Controller;


class NametxtController extends BaseController
{
    public function index()
    {
        $db_nametxt = M('nametxt');
        if (empty(I('get.search1'))) {
            $search = I('get.search1');
            $count1 = $data_nametxt = $db_nametxt->where("count = 1 and txt like '%" . $search . "%'")->count();
        } else {
            $count1 = $data_nametxt = $db_nametxt->where("count = 1")->count();
        }
        $tab = !empty($_GET['tab']) ? $_GET['tab'] : '1';
        $this->assign('tab', $tab);//选项卡
        $this->assign('count1', $count1);
        $this->display();
    }

    public function dataGet()
    {
        $type = I('post.type');
        $page = I('post.page');
        $limit = I('post.page_count');
        $search = I('post.search');
        $db_nametxt = M('nametxt');
        if ($type > 2) {
            if (!empty($search)) {
                $data_nametxt = $db_nametxt->where("status = 1 and count >2 and txt like '%" . $search . "%'")->order('hits DESC')->page($page, $limit)->select();
            } else {
                $data_nametxt = $db_nametxt->where("status = 1 and count >2")->page($page, $limit)->order('hits DESC')->select();
            }
        } else {
            if (!empty($search)) {
                $data_nametxt = $db_nametxt->where("status = 1 and count = " . $type . " and txt like '%" . $search . "%'")->order('hits DESC')->page($page, $limit)->select();
            } else {
                $data_nametxt = $db_nametxt->where("status = 1 and count = " . $type)->page($page, $limit)->order('hits DESC')->select();
            }
        }
        $this->ajaxReturn($data_nametxt);
    }

    public function dataCount()
    {
        $type = I('post.type');
        $search = trim(I('post.search'));
        $db_nametxt = M('nametxt');
        if ($type > 2) {
            if (!empty($search)) {
                $count = $db_nametxt->where("status = 1 and count >2  and txt like '%" . $search . "%'")->count();
            } else {
                $count = $db_nametxt->where("status = 1 and count >2 ")->count();
            }
        } else {
            if (!empty($search)) {
                $count = $db_nametxt->where("status = 1 and count = " . $type . " and txt like '%" . $search . "%'")->count();
            } else {
                $count = $db_nametxt->where("status = 1 and count = " . $type)->count();
            }
        }

        $data['count'] = $count;
        $this->ajaxReturn($data);
    }

    public function delTxt()
    {
        if (IS_POST) {
            $id = I('post.id');
            $db_name = M('nametxt');
            $data_name = $db_name->where('id = ' . $id)->find();
            $data_name['status'] = 0;
            $result = $db_name->save($data_name);
            if ($result !== false) {
                $this->success('success');
            }
        }
        $this->error('error');
    }

    public function txtAdd()
    {
        if (IS_POST) {
            $data['txt'] = I('post.txt');
            $data['count'] = mb_strlen($data['txt'], 'utf-8');
            $db_name = M('nametxt');
            $result = $db_name->add($data);
            if ($result !== false) {
                $this->success('success');
            }
        }
        $this->error('error');
    }

    public function addHits()
    {
        if (IS_POST) {
            $db_name = M('nametxt');
            $result = $db_name->where('id = ' . I('post.id'))->setInc('hits', 1);
            if ($result !== false) {
                $this->success('success');
            }
        }
        $this->error('error');
    }

    public function decHits()
    {
        if (IS_POST) {
            $db_name = M('nametxt');
            $result = $db_name->where('id = ' . I('post.id'))->setDec('hits', 1);
            if ($result !== false) {
                $this->success('success');
            }
        }
        $this->error('error');
    }

}